using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;

using UMEVOR.DTO;

using Microsoft.Practices.EnterpriseLibrary.Data;

namespace UMEVOR.DAL
{
    public class DALT_PERFIL : DALConnection
    {

        #region Incluir

        public void incluir(T_PERFIL obj)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();

                SQL.Append(" INSERT INTO T_PERFIL ");
                SQL.Append("    (TX_DSCR_PERF) ");
                SQL.Append(" VALUES ");
                SQL.Append("    ('" + obj.TX_DSCR_PERF  + "') ");

                DbCommand insertT_PERFIL = database.GetSqlStringCommand(SQL.ToString());

                insertT_PERFIL.CommandText = SQL.ToString();

                database.ExecuteNonQuery(insertT_PERFIL, DALConnection.transaction);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Incluir[]

        public void incluir(T_PERFIL[] obj)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();
                DbCommand[] insertT_PERFIL = new DbCommand[obj.Length];

                for (int index = 0; index < obj.Length; index++)
                {
                    T_PERFIL o = obj[index];
                    SQL = new StringBuilder();

                    SQL.Append(" INSERT INTO T_PERFIL ");
                    SQL.Append("    (ID_PERF, TX_DSCR_PERF) ");
                    SQL.Append(" VALUES ");
                    SQL.Append("    (:ID_PERF, :TX_DSCR_PERF) ");

                    insertT_PERFIL[index] = database.GetSqlStringCommand(SQL.ToString());

                    database.AddInParameter(insertT_PERFIL[index], ":ID_PERF", DbType.Int32, o.ID_PERF);
                    database.AddInParameter(insertT_PERFIL[index], ":TX_DSCR_PERF", DbType.String, o.TX_DSCR_PERF);

                    insertT_PERFIL[index].CommandText = SQL.ToString();
                }

                try
                {
                    foreach (DbCommand cmd in insertT_PERFIL)
                    {
                        database.ExecuteNonQuery(cmd, DALConnection.transaction);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Alterar

        public void alterar(T_PERFIL obj)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();

                SQL.Append(" UPDATE T_PERFIL SET ");
                SQL.Append("    TX_DSCR_PERF = '" + obj.TX_DSCR_PERF + "' ");
                SQL.Append(" WHERE ");
                SQL.Append("     ID_PERF = " + obj.ID_PERF.ToString());

                DbCommand updateT_PERFIL = database.GetSqlStringCommand(SQL.ToString());

                updateT_PERFIL.CommandText = SQL.ToString();

                database.ExecuteNonQuery(updateT_PERFIL, DALConnection.transaction);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Alterar[]

        public void alterar(T_PERFIL[] obj)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();
                DbCommand[] updateT_PERFIL = new DbCommand[obj.Length];

                for (int index = 0; index < obj.Length; index++)
                {
                    T_PERFIL o = obj[index];
                    SQL = new StringBuilder();
                    SQL.Append(" UPDATE T_PERFIL SET ");
                    SQL.Append("    TX_DSCR_PERF = :TX_DSCR_PERF ");
                    SQL.Append(" WHERE ");
                    SQL.Append("     ID_PERF = :ID_PERF  ");

                    updateT_PERFIL[index] = database.GetSqlStringCommand(SQL.ToString());

                    database.AddInParameter(updateT_PERFIL[index], ":ID_PERF", DbType.Int32, o.ID_PERF);
                    database.AddInParameter(updateT_PERFIL[index], ":TX_DSCR_PERF", DbType.String, o.TX_DSCR_PERF);

                    updateT_PERFIL[index].CommandText = SQL.ToString();
                }

                try
                {
                    foreach (DbCommand cmd in updateT_PERFIL)
                    {
                        database.ExecuteNonQuery(cmd, DALConnection.transaction);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Excluir

        public void excluir(T_PERFIL obj)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();

                SQL.Append(" DELETE FROM T_PERFIL ");
                SQL.Append(" WHERE ");
                SQL.Append("     ID_PERF = " + obj.ID_PERF.ToString());

                DbCommand deleteT_PERFIL = database.GetSqlStringCommand(SQL.ToString());

                deleteT_PERFIL.CommandText = SQL.ToString();

                database.ExecuteNonQuery(deleteT_PERFIL, DALConnection.transaction);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Excluir[]

        public void excluir(T_PERFIL[] obj)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();
                DbCommand[] deleteT_PERFIL = new DbCommand[obj.Length];

                for (int index = 0; index < obj.Length; index++)
                {
                    T_PERFIL o = obj[index];
                    SQL = new StringBuilder();
                    SQL.Append(" DELETE FROM T_PERFIL ");
                    SQL.Append(" WHERE ");
                    SQL.Append("     ID_PERF = :ID_PERF  ");

                    deleteT_PERFIL[index] = database.GetSqlStringCommand(SQL.ToString());

                    database.AddInParameter(deleteT_PERFIL[index], ":ID_PERF", DbType.Int32, o.ID_PERF);

                    deleteT_PERFIL[index].CommandText = SQL.ToString();
                }

                try
                {
                    foreach (DbCommand cmd in deleteT_PERFIL)
                    {
                        database.ExecuteNonQuery(cmd, DALConnection.transaction);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region ObterPorPK

        public T_PERFIL obterPorPK(int pID_PERF)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();

                SQL.Append(" SELECT * FROM T_PERFIL ");
                SQL.Append(" WHERE ");
                SQL.Append("  ID_PERF = " + pID_PERF.ToString() + " ORDER BY TX_DSCR_PERF");

                DbCommand consultaT_PERFIL = database.GetSqlStringCommand(SQL.ToString());
                consultaT_PERFIL.CommandText = SQL.ToString();

                IDataReader reader = database.ExecuteReader(consultaT_PERFIL, DALConnection.transaction);
                T_PERFIL obj = null;

                if (reader.Read())
                {
                    obj = new T_PERFIL();
                    if (!reader.IsDBNull(reader.GetOrdinal("ID_PERF")))
                    {
                        obj.ID_PERF = Int32.Parse(reader["ID_PERF"].ToString());
                    }
                    if (!reader.IsDBNull(reader.GetOrdinal("TX_DSCR_PERF")))
                    {
                        obj.TX_DSCR_PERF = reader["TX_DSCR_PERF"].ToString();
                    }
                }
                reader.Dispose();
                consultaT_PERFIL.Dispose();

                return obj;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region ListarTodos

        public List<T_PERFIL> listarTodos()
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();

                SQL.Append(" SELECT * FROM T_PERFIL ORDER BY TX_DSCR_PERF");

                DbCommand consultaT_PERFIL = database.GetSqlStringCommand(SQL.ToString());
                consultaT_PERFIL.CommandText = SQL.ToString();

                IDataReader reader = database.ExecuteReader(consultaT_PERFIL, DALConnection.transaction);
                List<T_PERFIL> lista = new List<T_PERFIL>();
                T_PERFIL obj = null;

                while (reader.Read())
                {
                    obj = new T_PERFIL();
                    if (!reader.IsDBNull(reader.GetOrdinal("ID_PERF")))
                    {
                        obj.ID_PERF = Int32.Parse(reader["ID_PERF"].ToString());
                    }
                    if (!reader.IsDBNull(reader.GetOrdinal("TX_DSCR_PERF")))
                    {
                        obj.TX_DSCR_PERF = reader["TX_DSCR_PERF"].ToString();
                    }
                    lista.Add(obj);
                }
                reader.Dispose();
                consultaT_PERFIL.Dispose();

                return lista;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<T_PERFIL> consultar(string descricao)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();

                SQL.Append(" SELECT * FROM T_PERFIL");
                SQL.Append(" WHERE TX_DSCR_PERF LIKE '" + descricao + "%' ORDER BY TX_DSCR_PERF");

                DbCommand consultaT_PERFIL = database.GetSqlStringCommand(SQL.ToString());
                consultaT_PERFIL.CommandText = SQL.ToString();

                IDataReader reader = database.ExecuteReader(consultaT_PERFIL, DALConnection.transaction);
                List<T_PERFIL> lista = new List<T_PERFIL>();
                T_PERFIL obj = null;

                while (reader.Read())
                {
                    obj = new T_PERFIL();
                    if (!reader.IsDBNull(reader.GetOrdinal("ID_PERF")))
                    {
                        obj.ID_PERF = Int32.Parse(reader["ID_PERF"].ToString());
                    }

                    if (!reader.IsDBNull(reader.GetOrdinal("TX_DSCR_PERF")))
                    {
                        obj.TX_DSCR_PERF = reader["TX_DSCR_PERF"].ToString();
                    }
                    lista.Add(obj);

                }
                reader.Dispose();
                consultaT_PERFIL.Dispose();

                return lista;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

    }
}

